<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%classroom_group}}".
 *
 * @property integer $id
 * @property string $name
 * @property integer $type
 * @property integer $created_at
 * @property integer $user_id
 */
class ClassroomGroup extends \zhtfrontend\components\bases\BaseModel
{
    public $classroom_id;
    public $classroom_ids;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%classroom_group}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type', 'created_at'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['name'], 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'name' => Yii::t('zhtfrontend', '教室组名'),
            'type' => Yii::t('zhtfrontend', '类型'),
            'user_id' => Yii::t('zhtfrontend', '操作人id'),
            'created_at' => Yii::t('zhtfrontend', '添加时间'),
        ];
    }

    public function extraFields()
    {
        return [
            'admin' => function () {
                return isset($this->admin->username)?$this->admin->username:'';
            },
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                        ['delete' => function ($data) {
                            $room = Classroom::find()->where(['classroom_group_id'=>$this->id])->one();
                            if($room != null){
                                return '<button class="yx-btn-disabled" type="button">删除</button>';
                            }else{
                                return '';
                            }

                          },
                        'update' => function ($data) {
                            return '';
                        }
                    ]
                )->create();
            },

        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['type'] = function () {
            switch ($this->type) {
                case 1:
                    return \Yii::t('zhtfrontend', '益智');
                case 2:
                    return \Yii::t('zhtfrontend', '音乐');
                case 3:
                    return \Yii::t('zhtfrontend', '形体');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDate($this->created_at);
        };
        return $fields;
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }


    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'user_id']);
    }

    public function getClassroom()
    {
        return $this->hasMany(Classroom::className(), ['classroom_group_id' => 'id']);
    }

}
